package cn.iocoder.yudao.module.crm.mq.consumer;

import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.mq.message.ImportTaskMessage;
import cn.iocoder.yudao.module.crm.service.importtask.ImportTaskService;
import cn.iocoder.yudao.module.system.mq.message.product.ProductSendMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 针对 {@link ProductSendMessage} 的消费者
 *
 * @author PetePower
 */
@Component
@Slf4j
public class ImportTaskConsumer {

    @Resource
    private ImportTaskService importTaskService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ImportTaskMessage message) {
        TenantUtils.execute(message.getTenantId(), () -> {
            importTaskService.executeByMq(message.getTaskId());
        });
    }

}
